package com.chongyuan.producer.quickServer;

import org.apache.commons.pool.PoolableObjectFactory;

public class GpsServerPoolableData extends GpsServerData implements PoolableObjectFactory<Object> {

	/**
	 * 重新初始化一个实例
	 * 
	 * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object)
	 */
	@Override
	public void activateObject(Object obj) throws Exception {
	}

	/**
	 * 销毁一个不再需要的实例
	 * 
	 * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object)
	 */
	@Override
	public void destroyObject(Object obj) throws Exception {
		if (obj == null) {
			return;
		}
		passivateObject(obj);
		obj = null;

	}

	/**
	 * 创建一个实例
	 * 
	 * @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
	 */
	@Override
	public Object makeObject() throws Exception {
		return new GpsServerPoolableData();
	}

	/**
	 * 未初始化实例返回到空闲对象池
	 * 
	 * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object)
	 */
	@Override
	public void passivateObject(Object obj) throws Exception {
		GpsServerPoolableData pd = (GpsServerPoolableData) obj;
		pd.setHelloCount(0);
		pd.setUsername(null);
	}

	/**
	 * 对象池返回的实例是否是安全的
	 * 
	 * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object)
	 */
	@Override
	public boolean validateObject(Object obj) {
		if (obj == null) {
			return false;
		} else {
			return true;
		}
	}

}
